﻿///***************************************************************
/// 시  스  템 : Updms.Com
/// 단위시스템 : COM
/// 계      층 : Presentation Layer
/// 프로그램명 : Com1012m
/// 파  일  명 : Com1012m.aspx.cs
/// 설      명 : 프로그램사용권한
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-07-28 송시명   최초 작성
///***************************************************************
using System;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Data;
using SMFramework.Utility;

using Base.AppFramework.Web.UI.CommandBar;
using Base.Dac;
using Updms.Com.Exe;


namespace Updms.Com
{
	public partial class Com1012m : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Com1012m";
		public static readonly string gs_path_info = "/updms/Com/" + PID + ".aspx";

		Global global = new Global();

		//메뉴 선택값
		protected string gs_sm = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetPgmInfo();
			SetPgmListByUserDiv();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion		

		#region SetInit

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";			

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		#endregion

		#region 디테일 Method (SetPgmInfo, SetPgmListByUserDiv)

		private void SetPgmInfo()
		{
			using (Com1012m_NT _Com1012m_NT = new Com1012m_NT())
			{
				lbox_left.DataSource = _Com1012m_NT.GetPgmInfo(LANG_SET);
				lbox_left.DataMember = "UPDMS_COM_PGM_GRANT";
				lbox_left.DataTextField = "Pgm_Nm";
				lbox_left.DataValueField = "Pgm_Id";
				lbox_left.DataBind();
			}
		}
		
		//구분별 리스트박스에 바인딩
		private void SetPgmListByUserDiv()
		{
			using (Com1012m_NT _Com1012m_NT = new Com1012m_NT())
			{
				//Super Administrator
				lbox_right_1.DataSource = _Com1012m_NT.GetPgmListByUserDiv("AA007", LANG_SET);
				lbox_right_1.DataMember = "UPDMS_COM_PGM_GRANT";
				lbox_right_1.DataTextField = "Pgm_Nm";
				lbox_right_1.DataValueField = "Pgm_Id";
				lbox_right_1.DataBind();

				//Administrator
				lbox_right_2.DataSource = _Com1012m_NT.GetPgmListByUserDiv("AA006", LANG_SET);
				lbox_right_2.DataMember = "UPDMS_COM_PGM_GRANT";
				lbox_right_2.DataTextField = "Pgm_Nm";
				lbox_right_2.DataValueField = "Pgm_Id";
				lbox_right_2.DataBind();

				//Readers
				lbox_right_3.DataSource = _Com1012m_NT.GetPgmListByUserDiv("AA005", LANG_SET);
				lbox_right_3.DataMember = "UPDMS_COM_PGM_GRANT";
				lbox_right_3.DataTextField = "Pgm_Nm";
				lbox_right_3.DataValueField = "Pgm_Id";
				lbox_right_3.DataBind();

				//Common Users
				lbox_right_4.DataSource = _Com1012m_NT.GetPgmListByUserDiv("AA004", LANG_SET);
				lbox_right_4.DataMember = "UPDMS_COM_PGM_GRANT";
				lbox_right_4.DataTextField = "Pgm_Nm";
				lbox_right_4.DataValueField = "Pgm_Id";
				lbox_right_4.DataBind();
			}
		}

		#endregion

		#region CRUD (InsertProcess, SearchProcess)

		//저장
		private void InsertProcess(DataPack lo_dp)
		{
			string ls_msg_id = "";
			using (Com1012m_RT _Com1012m_RT = new Com1012m_RT())
			{
				int li_return = _Com1012m_RT.InsertProcess(lo_dp);
				ls_msg_id = (li_return > 0) ? "MSG_INF_007" : "MSG_ERR_105";	//저장되었습니다:저장할 수 없습니다
				CallMsgBoxInAjax(ls_msg_id);
				
				SearchProcess();
			}
		}

		//검색
		private void SearchProcess()
		{
			SetPgmInfo();
			SetPgmListByUserDiv();
		}	
		
		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
					//사용권한별 버튼처리
					PagePreRender.SetVisibleCtrl(this, access.CheckPgmUsingGrant(USER_DIV, PID), access.GetVisibleCtrl(PID));
				}
			}
		}

		#endregion

		#region 버튼이벤트

		//검색
		protected void ibtn_search_Click(object sender, EventArgs e)
		{
			SearchProcess();
		}

		//저장
		protected void ibtn_save_Click(object sender, EventArgs e)
		{
			DataPack lo_dp = new DataPack();
			lo_dp.AddProperty("ls_xml", typeof(string), h_save_data.Value);
			lo_dp.AddProperty("ls_user_id", typeof(string), USER_ID);

			InsertProcess(lo_dp);
		}

		//Super Administrator추가
		protected void lbtn_add_sa_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < lbox_left.Items.Count; i++)
			{
				//하위메뉴만 && 중복등록불가 
				if (lbox_left.Items[i].Selected && lbox_left.Items[i].Text.Contains("[") && !WebControlUtil.IsExistValueOnListBox(lbox_right_1, lbox_left.Items[i].Value))
				{
					//after에 추가
					ListItem items = new ListItem(lbox_left.Items[i].Text, lbox_left.Items[i].Value);
					lbox_right_1.Items.Add(items);
				}
			}
		}

		//Administrators추가
		protected void lbtn_add_a_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < lbox_left.Items.Count; i++)
			{
				if (lbox_left.Items[i].Selected && lbox_left.Items[i].Text.Contains("[") && !WebControlUtil.IsExistValueOnListBox(lbox_right_2, lbox_left.Items[i].Value))
				{
					//after에 추가
					ListItem items = new ListItem(lbox_left.Items[i].Text, lbox_left.Items[i].Value);
					lbox_right_2.Items.Add(items);
				}
			}
		}

		//Readers추가
		protected void lbtn_add_r_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < lbox_left.Items.Count; i++)
			{
				if (lbox_left.Items[i].Selected && lbox_left.Items[i].Text.Contains("[") && !WebControlUtil.IsExistValueOnListBox(lbox_right_3, lbox_left.Items[i].Value))
				{
					//after에 추가
					ListItem items = new ListItem(lbox_left.Items[i].Text, lbox_left.Items[i].Value);
					lbox_right_3.Items.Add(items);
				}
			}
		}

		//Common Users추가
		protected void lbtn_add_cu_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < lbox_left.Items.Count; i++)
			{
				if (lbox_left.Items[i].Selected && lbox_left.Items[i].Text.Contains("[") && !WebControlUtil.IsExistValueOnListBox(lbox_right_4, lbox_left.Items[i].Value))
				{
					//after에 추가
					ListItem items = new ListItem(lbox_left.Items[i].Text, lbox_left.Items[i].Value);
					lbox_right_4.Items.Add(items);
				}
			}
		}
		
		#endregion
		
	}
}
